/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import org.balisunrise.application.Application;
import org.openide.util.ImageUtilities;

/**
 * Implementação de {@link org.balisunrise.common.IconManager}.
 * São geredos icones do tipo {@link javax.swing.ImageIcon}.
 * Os icones padrão são carregados do arquivo
 * {@code /org/balisunrise/resources/Bundle.properties}.
 * @author glauco
 */
public final class IconManager
        implements org.balisunrise.common.IconManager<ImageIcon>{

    /**
     * Mapa coms os icones criados.
     */
    HashMap<String, ImageIcon> icons = new HashMap<String, ImageIcon>();

    /**
     * Mapa com os paths para os icones.
     */
    HashMap<String, String> paths = new HashMap<String, String>();

    /**
     * Contrutor busca os icones padrão do sistema do aquivo
     * {@code /org/balisunrise/resources/Bundle.properties}.
     */
    public IconManager() {
        try{
            Properties p = new Properties();
            p.load(IconManager.class.getResourceAsStream(
                    "/org/balisunrise/resources/Bundle.properties"));
            for (Entry<Object, Object> e : p.entrySet()) {
                addIcon((String)e.getKey(), (String)e.getValue());
            }
        }catch(IOException ex){
            Application.get().getLogger().log(Level.SEVERE, 
                    "Can't load default icons!", ex);
        }
    }

    /**
     * Pega um icone pelo nome, ou pelo path do arquivo.
     */
    @Override
    public ImageIcon getIcon(String nameOrPath) {

        if(nameOrPath == null)
            return null;

        if(icons.containsKey(nameOrPath)){
            return icons.get(nameOrPath);
        }

        String path = nameOrPath;
        if(paths.containsKey(nameOrPath))
            path = paths.get(nameOrPath);

        ImageIcon icon = ImageUtilities.loadImageIcon(path, true);

        if (icon != null)
            icons.put(nameOrPath, icon);

        return icon;
    }

    /**
     * Adiciona um icone.
     * @param name nome do icone.
     * @param path caminho do arquivo.
     */
    @Override
    public void addIcon(String name, String path) {
        if(name == null)
            return ;
        if(paths.containsKey(name))
            return ;
        if(path ==  null)
            return ;
        paths.put(name, path);
    }

    /**
     * Retorna o Path do Icone
     * @param name Nome do Icone.
     * @return String do Path do Icone.
     */
    public String getPath(String name){
        if(!paths.containsKey(name))
            return "";
        return paths.get(name);
    }

}
